package util;

import java.security.*;
import java.math.*;

public class MD5 {
    
	public MD5() {}
	
//	public static String encrypt(String input)  {
//		String encrypt = null;
//		try {
//			MessageDigest m = MessageDigest.getInstance("MD5");
//			m.update(input.getBytes(), 0, input.length());
//			encrypt = new BigInteger(1, m.digest()).toString(16);
//			while ( encrypt.length() < 32 ) {
//				encrypt = "0"+ encrypt;
//			}
//
//		} catch (NoSuchAlgorithmException x) {};
//		return encrypt;
//	}
	
	public static String encrypt(String input) {
        String result = input;
        try {
        	if(input != null) {
                MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
                md.update(input.getBytes());
                BigInteger hash = new BigInteger(1, md.digest());
                result = hash.toString(16);
                if ((result.length() % 2) != 0) {
                    result = "0" + result;
                }
            }
		} catch (NoSuchAlgorithmException e) {}
        return result;
    }

	public static void main(String[] args) throws Exception {
        System.out.println(MD5.encrypt("1234"));
    }
}